/**
 * 作者: wgx
 * 日期: 2025-04-15 17:45:51
 * 备注: 单服派对表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.CsGameFeast.PBServerFeast;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class ServerFeastModel extends BaseModel implements Model {
    public static final String LISTKEY = "server_id.uniq_key";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_NOT;// 数据入库类型 不入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // ID
    protected int serverId; // 服务器Id
    protected String uniqKey = ""; // 举办者用户id_派对id 组成的唯一值
    protected long userId; // 举办者用户id
    protected int feastMainId; // 派对id
    protected int score; // 总人气
    protected int guestNum; // 参加人数
    protected String userName = ""; // 举办者用户名字
    protected int level; // 举办者等级
    protected int vip; // 举办者vip等级
    protected String icon = ""; // 举办者的头像
    protected long addTime; // 举办时间
    protected int isThree; // 是否3倍奖励
    protected String joinTypeAllow = ""; // 普通派对允许的参与类型
    protected long joinTime; // 最后的加入时间，判断是否生成机器人

    public void reset() {
    }

    protected String getKey() {
        return "ServerFeast." + serverId + "." + uniqKey;
    }

    protected String getListKey() {
        return "ServerFeast." + serverId;
    }

    protected String getListItemKey() {
        return uniqKey + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("server_feast"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, 0, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // ID
        obj.put("serverId", serverId); // 服务器Id
        obj.put("uniqKey", uniqKey); // 举办者用户id_派对id 组成的唯一值
        obj.put("userId", userId); // 举办者用户id
        obj.put("feastMainId", feastMainId); // 派对id
        obj.put("score", score); // 总人气
        obj.put("guestNum", guestNum); // 参加人数
        obj.put("userName", userName); // 举办者用户名字
        obj.put("level", level); // 举办者等级
        obj.put("vip", vip); // 举办者vip等级
        obj.put("icon", icon); // 举办者的头像
        obj.put("addTime", addTime); // 举办时间
        obj.put("isThree", isThree); // 是否3倍奖励
        obj.put("joinTypeAllow", joinTypeAllow); // 普通派对允许的参与类型
        obj.put("joinTime", joinTime); // 最后的加入时间，判断是否生成机器人
        return obj;
    }

    public PBServerFeast.Builder takeInitBuilder() {
        PBServerFeast.Builder sb = PBServerFeast.newBuilder();
        if (id != 0) {
            sb.setId(id);
        }
        if (userId != 0) {
            sb.setUserId(userId);
        }
        if (feastMainId != 0) {
            sb.setFeastMainId(feastMainId);
        }
        if (score != 0) {
            sb.setScore(score);
        }
        if (guestNum != 0) {
            sb.setGuestNum(guestNum);
        }
        if (!userName.isEmpty()) {
            sb.setUserName(userName);
        }
        if (level != 0) {
            sb.setLevel(level);
        }
        if (vip != 0) {
            sb.setVip(vip);
        }
        if (!icon.isEmpty()) {
            sb.setIcon(icon);
        }
        if (addTime != 0) {
            sb.setAddTime(addTime);
        }
        if (this.joinTypeAllow != null && this.joinTypeAllow.length() > 0) {
            JsonArray list = new JsonArray(this.joinTypeAllow);
            for(int i=0; i<list.size(); i++) {
                sb.addJoinTypeAllow(list.getInteger(i));
            }
        }
        return sb;
    }

    public PBServerFeast.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBServerFeast.Builder sb = PBServerFeast.newBuilder();
        if (isFieldUpdate("id")) {
            sb.setId(id);
        }
        if (isFieldUpdate("userId")) {
            sb.setUserId(userId);
        }
        if (isFieldUpdate("feastMainId")) {
            sb.setFeastMainId(feastMainId);
        }
        if (isFieldUpdate("score")) {
            sb.setScore(score);
        }
        if (isFieldUpdate("guestNum")) {
            sb.setGuestNum(guestNum);
        }
        if (isFieldUpdate("userName")) {
            sb.setUserName(userName);
        }
        if (isFieldUpdate("level")) {
            sb.setLevel(level);
        }
        if (isFieldUpdate("vip")) {
            sb.setVip(vip);
        }
        if (isFieldUpdate("icon")) {
            sb.setIcon(icon);
        }
        if (isFieldUpdate("addTime")) {
            sb.setAddTime(addTime);
        }
        if (isFieldUpdate("joinTypeAllow")) {
            if (this.joinTypeAllow != null && this.joinTypeAllow.length() > 0) {
                JsonArray list = new JsonArray(this.joinTypeAllow);
                for(int i=0; i<list.size(); i++) {
                    sb.addJoinTypeAllow(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearJoinTypeAllow();
                }
            } else {
                sb.clearJoinTypeAllow();
            }
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public int getServerId() {
        return serverId;
    }

    public void putServerId(int serverId) {
        if (this.serverId != serverId) {
            this.serverId = serverId;
            updateRedisHashField(getKey(), "serverId", this.serverId);
        }
    }

    public void addServerId(int value) {
        this.serverId += value;
        updateRedisHashField(getKey(), "serverId", this.serverId);
    }

    public String getUniqKey() {
        return uniqKey;
    }

    public void putUniqKey(String uniqKey) {
        if (!this.uniqKey.equals(uniqKey)) {
            this.uniqKey = uniqKey;
            updateRedisHashField(getKey(), "uniqKey", this.uniqKey);
        }
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public int getFeastMainId() {
        return feastMainId;
    }

    public void putFeastMainId(int feastMainId) {
        if (this.feastMainId != feastMainId) {
            this.feastMainId = feastMainId;
            updateRedisHashField(getKey(), "feastMainId", this.feastMainId);
        }
    }

    public void addFeastMainId(int value) {
        this.feastMainId += value;
        updateRedisHashField(getKey(), "feastMainId", this.feastMainId);
    }

    public int getScore() {
        return score;
    }

    public void putScore(int score) {
        if (this.score != score) {
            this.score = score;
            updateRedisHashField(getKey(), "score", this.score);
        }
    }

    public void addScore(int value) {
        this.score += value;
        updateRedisHashField(getKey(), "score", this.score);
    }

    public int getGuestNum() {
        return guestNum;
    }

    public void putGuestNum(int guestNum) {
        if (this.guestNum != guestNum) {
            this.guestNum = guestNum;
            updateRedisHashField(getKey(), "guestNum", this.guestNum);
        }
    }

    public void addGuestNum(int value) {
        this.guestNum += value;
        updateRedisHashField(getKey(), "guestNum", this.guestNum);
    }

    public String getUserName() {
        return userName;
    }

    public void putUserName(String userName) {
        if (!this.userName.equals(userName)) {
            this.userName = userName;
            updateRedisHashField(getKey(), "userName", this.userName);
        }
    }

    public int getLevel() {
        return level;
    }

    public void putLevel(int level) {
        if (this.level != level) {
            this.level = level;
            updateRedisHashField(getKey(), "level", this.level);
        }
    }

    public void addLevel(int value) {
        this.level += value;
        updateRedisHashField(getKey(), "level", this.level);
    }

    public int getVip() {
        return vip;
    }

    public void putVip(int vip) {
        if (this.vip != vip) {
            this.vip = vip;
            updateRedisHashField(getKey(), "vip", this.vip);
        }
    }

    public void addVip(int value) {
        this.vip += value;
        updateRedisHashField(getKey(), "vip", this.vip);
    }

    public String getIcon() {
        return icon;
    }

    public void putIcon(String icon) {
        if (!this.icon.equals(icon)) {
            this.icon = icon;
            updateRedisHashField(getKey(), "icon", this.icon);
        }
    }

    public long getAddTime() {
        return addTime;
    }

    public void putAddTime(long addTime) {
        if (this.addTime != addTime) {
            this.addTime = addTime;
            updateRedisHashField(getKey(), "addTime", this.addTime);
        }
    }

    public void addAddTime(long value) {
        this.addTime += value;
        updateRedisHashField(getKey(), "addTime", this.addTime);
    }

    public int getIsThree() {
        return isThree;
    }

    public void putIsThree(int isThree) {
        if (this.isThree != isThree) {
            this.isThree = isThree;
            updateRedisHashField(getKey(), "isThree", this.isThree);
        }
    }

    public void addIsThree(int value) {
        this.isThree += value;
        updateRedisHashField(getKey(), "isThree", this.isThree);
    }

    public JsonArray getJoinTypeAllowArray() {
        if (this.joinTypeAllow != null && this.joinTypeAllow.length() > 0) {
            return new JsonArray(this.joinTypeAllow);
        } else {
            return new JsonArray();
        }
    }

    public String getJoinTypeAllow() {
        return joinTypeAllow;
    }

    public void putJoinTypeAllow(String joinTypeAllow) {
        if (!this.joinTypeAllow.equals(joinTypeAllow)) {
            this.joinTypeAllow = joinTypeAllow;
            updateRedisHashField(getKey(), "joinTypeAllow", this.joinTypeAllow);
        }
    }

    public long getJoinTime() {
        return joinTime;
    }

    public void putJoinTime(long joinTime) {
        if (this.joinTime != joinTime) {
            this.joinTime = joinTime;
            updateRedisHashField(getKey(), "joinTime", this.joinTime);
        }
    }

    public void addJoinTime(long value) {
        this.joinTime += value;
        updateRedisHashField(getKey(), "joinTime", this.joinTime);
    }

}